package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.WemediaFeignClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojo.ApUser;
import com.heima.model.user.pojo.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.UserRealNameMapper;
import com.heima.user.service.UserRealNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserRealNameServiceImpl extends ServiceImpl<UserRealNameMapper, ApUserRealname> implements UserRealNameService {
    @Autowired
    private UserRealNameMapper userRealNameMapper;
    @Autowired
    private WemediaFeignClient wemediaFeignClient;
    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 查询用户认证信息
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult findAll(AuthDto authDto) {
        //1.校验参数
        if (authDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2.创建查询条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        //3.创建分页对象
        Page<ApUserRealname> page = new Page(authDto.getPage(),authDto.getSize());

        //4.判断是否要查询状态
        if (authDto.getStatus() != null){
            queryWrapper.eq(ApUserRealname::getStatus,authDto.getStatus());
        }

        //5.查询
        Page<ApUserRealname> apUserRealnamePage = userRealNameMapper.selectPage(page, queryWrapper);
        PageResponseResult result = new PageResponseResult(authDto.getPage(),authDto.getSize(), (int) apUserRealnamePage.getTotal());
        result.setData(apUserRealnamePage.getRecords());

        return result;
    }

    /**
     * 用户认证通过
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto authDto) {
        //1.参数校验
        if (authDto == null || authDto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.根据用户id查询用户信息
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserRealname::getUserId,authDto.getId());
        ApUserRealname apUserRealname = userRealNameMapper.selectOne(queryWrapper);

        //3.判断用户是否存在
        if (apUserRealname == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //4.修改用户信息
        apUserRealname.setUpdatedTime(new Date());
        apUserRealname.setSubmitedTime(new Date());
        apUserRealname.setStatus((short)9);
        userRealNameMapper.updateById(apUserRealname);

        //5.用户审核通过，保存用户的信息到自媒体用户表
        //查询审核用户的信息
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        wemediaFeignClient.saveWmUser(apUser);

        return ResponseResult.okResult(apUserRealname);
    }

    /**
     * 用户审核失败
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        //1.参数校验
        if (authDto == null || authDto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.根据用户id查询用户信息
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserRealname::getUserId,authDto.getId());
        ApUserRealname apUserRealname = userRealNameMapper.selectOne(queryWrapper);

        //3.判断用户是否存在
        if (apUserRealname == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //4.修改用户信息
        apUserRealname.setStatus((short)2);
        apUserRealname.setUpdatedTime(new Date());
        userRealNameMapper.updateById(apUserRealname);

        return ResponseResult.okResult("用户审核失败成功！");
    }
}
